package cn.yql8.controller.web;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.yql8.bean.Group;
import cn.yql8.bean.User;
import cn.yql8.service.GroupService;
import cn.yql8.service.UserService;
import cn.yql8.util.LuceneUtil;


@Controller
@RequestMapping("/index")
public class HelloController {
	private Log logger = LogFactory.getLog(HelloController.class);
	
	@Resource(name="userService")
	private UserService userService;
	
	@Resource(name="groupService")
	private GroupService groupService;
	
	@RequestMapping("id/{id}/gid/{gid}.html")
	public ModelAndView index(@PathVariable int id,@PathVariable int gid){
		ModelAndView mav = new ModelAndView();

		Group group = new Group();
		group.setPid(0);
		group.setName("管理员");
		
		User user = new User();
		user.setAccount("chenbin");
		user.setGroup(group);
		user.setName("陈彬");
		user.setPwd("123456");
		
		this.userService.addGroupAndUser(group, user);
		
		mav.setViewName("index");
		return mav;
	}
	@RequestMapping("hello.html")
	public String hello(){
		return "hello";
	}
	@RequestMapping("test.html")
	public void test(){
        /*
		try {
			String str = "经中央军委主席习近平批准，总政治部、总后勤部、军委纪委日前联合发出通知，决定利用两年时间，在全军和武警部队组织开展经济适用住房建设项目专项清理整治，旨在妥善解决住房建设管理中存在的遗留问题，彻底纠治住房方面的腐败问题和不正之风，坚决维护政策纪律的权威性和严肃性";
			StringReader reader = new StringReader(str); 
			IKSegmenter ik = new IKSegmenter(reader,false);//当为true时，分词器进行最大词长切分 
			Lexeme lexeme = null; 
			while((lexeme = ik.next())!=null){
				System.out.println(lexeme.getLexemeText()); 
			} 
			Term term = new Term("id", "1999991");
			Document doc = new Document();   
			doc.add(new IntField("id",1999991, Store.YES));   
			doc.add(new StringField("name", 555555 + "555555" + 555555, Store.YES));
			doc.add(new TextField("content", str, Store.NO));
			IndexWriter writer = LuceneUtil.getWriter(User.class);
			writer.updateDocument(term, doc);
			writer.commit();//凡是涉及到索引变化的动作都要提交才能生效
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
        */
	}
	@RequestMapping(value="json.asp")
	@ResponseBody
	public Object json(){
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("state", 1);
		map.put("msg", "你好啊中国啊");
		User user = new User();
		user.setId(1);
		user.setName("陈彬");
		map.put("user", user);

		return map;
	}
}